<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/9
 * Time: 11:39
 */

namespace common\utils;

use Yii;
class PayUtils
{

    public static function getAlipay(){
        $alipay_config['partner']		= Yii::$app->params['partner'];
        $alipay_config['seller_id']		= Yii::$app->params['seller_id'];
        $alipay_config['private_key']	=  Yii::$app->params['private_key_path'];
        $alipay_config['ali_public_key']=  Yii::$app->params['ali_public_key_path'];
        $alipay_config['private_key_path']	= getcwd().'/frontend/pay/alipay/key/rsa_private_key.pem';
        $alipay_config['ali_public_key_path']= getcwd().'/frontend/pay/alipay/key/alipay_public_key.pem';
        $alipay_config['sign_type']    = strtoupper('RSA');
        $alipay_config['input_charset']= strtolower('utf-8');
        $alipay_config['cacert']    = Yii::$app->params['cacert'];
        $alipay_config['transport']    = 'http';
        return $alipay_config;
    }

    //获取ali配置
    public static function getAliConfig()
    {
        $alipay_config['partner']		= Yii::$app->params['partner'];
        $alipay_config['seller_id']		= Yii::$app->params['seller_id'];
        $alipay_config['private_key_path']	= Yii::$app->params['private_key_path'];
        $alipay_config['ali_public_key_path']= Yii::$app->params['ali_public_key_path'];
        $alipay_config['sign_type']    = strtoupper('RSA');
        $alipay_config['input_charset']= strtolower('utf-8');
        $alipay_config['cacert']    = Yii::$app->params['cacert'];
        $alipay_config['transport']    = 'http';
        return $alipay_config;
    }

    /**
     * RSA签名
     * @param string $data 待签名数据
     * @param string $private_key_path 商户私钥文件路径
     * return 签名结果
     */
    public static function rsaSign($data, $private_key_path) {
        $priKey = file_get_contents($private_key_path);
        $res = openssl_get_privatekey($priKey);
        openssl_sign($data, $sign, $res);
        openssl_free_key($res);
        //base64编码
        $sign = base64_encode($sign);
        $sign = urlencode($sign);
        return $sign;
    }

    /**
     * RSA验签
     * @param string $data 待签名数据
     * @param string $ali_public_key_path 支付宝的公钥文件路径
     * @param string $sign 要校对的的签名结果
     * return 验证结果
     */
    public static function rsaVerify($data, $ali_public_key_path, $sign)  {
        $pubKey = file_get_contents($ali_public_key_path);
        $res = openssl_get_publickey($pubKey);
        $result = (bool)openssl_verify($data, base64_decode($sign), $res);
        openssl_free_key($res);
        return $result;
    }



    //生成微信签名
    public static function makeWechatSign($payInfo)
    {
        $para_filter = Utils::paraFilter($payInfo);
        $para_sort = Utils::argSort($para_filter);
        $prestr = Utils::createLinkstring($para_sort);
        $prestr .= "&key=".Yii::$app->params['weixin_key'];
        return strtoupper(MD5($prestr));
    }

    //校验微信返回值
    public static function checkWechatReturn($orderResult){
        $returnSign = $orderResult['sign'];
        unset($orderResult['sign']);
        $sign = self::makeWechatSign($orderResult);
        return $returnSign == $sign ? 1 : 0;
    }
}